package ultweb.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ultweb.domain.Prodotto;
import ultweb.service.ProductManager;
import ultweb.service.Aggiorna;

@Controller
@RequestMapping("/aggiorna")
public class AggiornaController {

    protected final Log logger = LogFactory.getLog(getClass());
    private String me="AggiornaController-";
    
    @Autowired
    private ProductManager productManager;
    private Map<String, Object> myModel;
  
    
    @RequestMapping(value = "/aggiorna.html", method = RequestMethod.GET)
    public ModelAndView handleRequest(){
    	String foo=me +"handleRequest- ";
    	logger.info(foo+"begin ");
    	
        String now = (new java.util.Date()).toString();
        
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("now", now);
        myModel.put("prodotti", this.productManager.getProdotti());
        
        
        logger.info(foo+"End "+myModel);
    	return new ModelAndView("aggiorna", "model", myModel);
    	
    }
    
    @RequestMapping( method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute("aggiorna")Aggiorna aggiornaProdotto,   ModelMap model) {
    	String foo=me +"processSubmit- ";
    	
    	String marca = aggiornaProdotto.getMarca();
        Integer id = aggiornaProdotto.getId();
        
        
        logger.info(foo+"Marca = " +marca);
        logger.info(foo+" Id = " +id);
    	
        if (id!= null){
        	Prodotto prod= productManager.getProdottoByID(id);
        	logger.info("hallo!!!!! "+prod);
        	productManager.aggiornaProdotto(prod);
        }
      
        logger.info(foo+"Marca = " +marca);
        logger.info(foo+" Id = " +id);
        
       return "redirect:/hello.htm";
    }
  
    @RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
    	String foo=me +"initForm- ";
    	logger.info(foo+" begin ");
    	Aggiorna aggP = new Aggiorna();
    	aggP.setMarca("pippo");
    	aggP.setId(new Integer(0));
    	logger.info(foo+" end "+ aggP);
    	
    	model.addAttribute("aggiorna", aggP);
		//return form view
		return "aggiorna";
	}

    /*public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }
    
    public ProductManager getProductManager() {
        return productManager;
    }
    */
    
    
    @ModelAttribute("prodottiList")
	public Map<String,String> populateCountryList() {
		
    	List<Prodotto> listaProd=productManager.getProdotti();
    	Map<String,String> prodMap = new LinkedHashMap<String,String>();
		
    	for(Prodotto prod : listaProd){
    		prodMap.put(Integer.toString(prod.getIdProdotto()),prod.getMarcaProdotto());
    	}
    	
		//Data referencing for java skills list box
//    	prodMap.put("US", "United Stated");
//    	prodMap.put("CHINA", "China");
//    	prodMap.put("SG", "Singapore");
//		prodMap.put("MY", "Malaysia");
//		
		
		return prodMap;
	}
    
    @InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		
	}
    
}